package org.batch.config.batch;

import org.batch.entity.Trading;
import org.batch.utils.ObjectUtil;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * 批处理相关配置类
 *
 * @author LiKun
 * @date 2021/9/27 16:56
 */
@Configuration
public class BatchConfig {

    /**
     * 数据库类型-MySQL
     */
    public static final String DB_TYPE_MYSQL = "mysql";

    /**
     * 批处理任务调动启动器
     *
     * @param dataSource         数据源
     * @param transactionManager 事务管理器
     *
     * @return SimpleJobLauncher
     *
     * @throws Exception 相关异常
     */
    @Bean
    public SimpleJobLauncher simpleJobLauncher(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository(dataSource, transactionManager));

        return jobLauncher;
    }

    /**
     * 注册数据持久化的存储库
     *
     * @param dataSource         数据源
     * @param transactionManager 事务管理器
     *
     * @return jobRepository
     *
     * @throws Exception 相关异常
     */
    @Bean
    public JobRepository jobRepository(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception {
        JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();

        factoryBean.setDataSource(dataSource);
        factoryBean.setDatabaseType(DB_TYPE_MYSQL);
        factoryBean.setTransactionManager(transactionManager);

        return factoryBean.getObject();
    }

    /**
     * CSV文件读取器
     *
     * @param path 文件路径
     *
     * @return fileItemReader
     */
    @Bean
    @StepScope
    public FlatFileItemReader flatFileItemReader(@Value("#{jobParameters[path]}") String path,
                                                 @Value("#{jobParameters[fields]}") String fields) {
        FlatFileItemReader<Trading> fileItemReader = new FlatFileItemReader<>();

        fileItemReader.setLineMapper(new DefaultLineMapper<Trading>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(ObjectUtil.parse2Array(fields));
            }});

            setFieldSetMapper(new BeanWrapperFieldSetMapper<Trading>() {{
                setTargetType(Trading.class);
            }});
        }});
        fileItemReader.setResource(new ClassPathResource(path));

        return fileItemReader;
    }

}
